<?php

namespace app\command;

use app\admin\model\Channel;
use app\admin\model\Custom;
use app\admin\model\Maint;
use app\admin\model\Other;
use app\admin\model\Project;
use app\admin\model\ProjectModel;
use app\admin\model\Taobao;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Statistics extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Statistics');
        // 设置参数
        $this->setDescription('数据统计')->setHelp("php think Statistics");
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始执行');
        //查询总收入
        $total_amount = 0;
        $income_amount = 0;
        $project = (new Project())->order('id desc')->select();
        foreach ($project as $items) {
            $total_amount += $items['total_money'];
            $income = json_decode($items['income'], true);
            if ($income) {
                $income_amount += array_sum(array_column($income, 'amount'));
            }
        }
        $data['total_amount'] = $total_amount;    //项目总收入
        $data['total_income'] = $income_amount;   //项目实际到账
        $data['taobao_cost'] = (new Taobao())->sum('total_money') ?? 0;  //淘宝刷单
        $data['channel_amount'] = (new Channel())->sum('income') ?? 0;   //渠道收入
        $data['other_cost'] = (new Other())->sum('money') ?? 0;          //其他支出
        $data['maint_amount'] = (new Maint())->sum('money') ?? 0;        //维护总收入

        $data['custom_nums'] = (new Custom())->count() ?? 0;       //客户数量
        $data['project_nums'] = (new Project())->count() ?? 0;       //项目总数量

        $data['update_time'] = time();

        (new \app\admin\model\Statistics())->where(['id' => 1])->update($data);

        $output->writeln('结束执行');

    }
}